我們來看到C++要如何計算程式碼的執行時間呢 ~
學習目標: 計算程式碼執行時間的實務
學習難度: ☆☆☆
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{
auto start=high_resolution_clock::now(); //開始
for (int i = 0; i < 10000; ++i)
{
cout<<"123"<<endl;
}
auto stop = high_resolution_clock::now(); //結束
auto duration = duration_cast<milliseconds>(stop - start);
cout <<"Time taken by" <<duration.count() <<"milliseconds"<< endl; //豪秒
return 0;
}
參考資料:
https://www.geeksforgeeks.org/measure-execution-time-function-cpp/